<!-- subpkg_archive/list/index.vue -->
<script lang="ts" setup>
  import { getPatientListAPI } from '@/api/patientAPI'
  import { useConsultStore } from '@/stores/consult'
  import type { PatientItem } from '@/type/patient'
  import { onShow } from '@dcloudio/uni-app'
  import { ref } from 'vue'

  const swipeOptions = ref([
    {
      text: '删除',
      style: {
        backgroundColor: '#dd524d',
        color: '',
        fontSize: '',
      },
    },
  ])
  const store = useConsultStore()
  const activeIndex = ref()

  interface Props {
    consult: boolean
  }
  const props = defineProps<Props>()

  if (props.consult) {
    uni.setNavigationBarTitle({
      title: '选择患者',
    })
  }
  // 1. 患者列表
  const patientList = ref<PatientItem[]>([])
  // 家庭档案（患者）列表
  async function getPatientList() {
    // 患者列表接口
    const data = await getPatientListAPI()
    // 渲染接口数据
    patientList.value = data
    activeIndex.value = patientList.value.findIndex((item) => item.defaultFlag == 1)
    store.patientId = patientList.value[activeIndex.value].id
  }
  onShow(() => {
    getPatientList()
  })
  const select = (index: number) => {
    if (!props.consult) return
    activeIndex.value = index
    store.patientId = patientList.value[index].id
  }
  const onNextStepClick = () => {
    uni.navigateTo({
      url: '/subpkg_consult/payment/payment',
    })
  }
</script>

<template>
  <scroll-view>
    <view class="archive-page">
      <view class="archive-tips">最多可添加6人</view>
      <uni-swipe-action>
        <uni-swipe-action-item
          v-for="(patient, index) in patientList"
          :key="patient.id"
          :right-options="swipeOptions"
        >
          <view
            :class="{ active: index == activeIndex }"
            class="archive-card"
            @click="select(index)"
          >
            <view class="archive-info">
              <text class="name">{{ patient.name }}</text>
              <text class="id-card">
                {{ patient.idCard.replace(/^(.{6}).+(.{4})$/, '$1********$2') }}
              </text>
              <text v-if="patient.defaultFlag === 1" class="default">默认</text>
            </view>
            <view class="archive-info">
              <text class="gender">{{ patient.genderValue }}</text>
              <text class="age">{{ patient.age }}岁</text>
            </view>
            <navigator
              hover-class="none"
              class="edit-link"
              :url="`/subpkg_archive/form/index?id=${patient.id}`"
            >
              <uni-icons type="icon-edit" size="20" color="#16C2A3" custom-prefix="iconfont" />
            </navigator>
          </view>
        </uni-swipe-action-item>
      </uni-swipe-action>

      <!-- 添加按钮 -->
      <view v-if="patientList?.length < 6" class="archive-card">
        <navigator class="add-link" hover-class="none" url="/subpkg_archive/form/form">
          <uni-icons color="#16C2A3" size="24" type="plusempty" />
          <text class="label">添加患者</text>
        </navigator>
      </view>
      <view class="next-step" v-if="props.consult">
        <button @click="onNextStepClick" class="uni-button">下一步</button>
      </view>
    </view>
  </scroll-view>
</template>

<style lang="scss">
  @import './list.scss';
</style>
